load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_python//python:defs.bzl", "py_library")
load("@py_deps//:requirements.bzl", "requirement")
load("//pylib:protobuf.bzl", "py_proto_library_typed")
load("@rules_python//experimental/python:wheel.bzl", "py_package", "py_wheel")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("//:defs.bzl", "anki_version")
load("//pylib:orjson.bzl", "orjson_if_available")

copy_file(
    name = "buildinfo",
    src = "//:buildinfo.txt",
    out = "buildinfo.txt",
)

genrule(
    name = "rsbackend_gen",
    outs = ["rsbackend_gen.py"],
    cmd = "$(location //pylib/tools:genbackend) > $@",
    tools = ["//pylib/tools:genbackend"],
)

genrule(
    name = "hooks_gen",
    outs = ["hooks_gen.py"],
    cmd = "$(location //pylib/tools:genhooks) $@",
    tools = ["//pylib/tools:genhooks"],
)

py_proto_library_typed(
    name = "backend_pb2",
    src = "//rslib:backend.proto",
    visibility = [
        "//visibility:public",
    ],
)

py_proto_library_typed(
    name = "fluent_pb2",
    src = "//rslib:fluent.proto",
    visibility = [
        "//visibility:public",
    ],
)

copy_file(
    name = "rsbridge_unix",
    src = "//pylib/rsbridge",
    out = "_rsbridge.so",
)

copy_file(
    name = "rsbridge_win",
    src = "//pylib/rsbridge",
    out = "_rsbridge.pyd",
)

alias(
    name = "rsbridge",
    actual = selects.with_or({
        (
            "@io_bazel_rules_rust//rust/platform:x86_64-pc-windows-msvc",
        ): ":rsbridge_win",
        "//conditions:default": ":rsbridge_unix",
    }),
    visibility = ["//visibility:public"],
)

_py_srcs = glob(
    ["**/*.py"],
    exclude = [
        "rsbackend_gen.py",
        "hooks_gen.py",
    ],
)

py_library(
    name = "anki",
    srcs = _py_srcs,
    data = [
        "py.typed",
        ":backend_pb2",
        ":buildinfo",
        ":fluent_pb2",
        ":hooks_gen",
        ":rsbackend_gen",
        ":rsbridge",
    ],
    imports = [
        "..",
    ],
    visibility = ["//visibility:public"],
    deps = [
        requirement("beautifulsoup4"),
        requirement("decorator"),
        requirement("distro"),
        requirement("protobuf"),
        requirement("requests"),
        requirement("flask"),
        requirement("waitress"),
    ] + orjson_if_available(),
)

py_package(
    name = "anki_pkg",
    packages = ["pylib.anki"],
    deps = [":anki"],
)

py_wheel(
    name = "wheel",
    abi = "abi3",
    description_file = "wheel_description.txt",
    distribution = "anki",
    extra_requires = {
        "syncserver": [
            "flask",
            "waitress",
        ],
    },
    platform = select({
        "//platforms:windows_x86_64": "win_amd64",
        "//platforms:macos_x86_64": "macosx_10_7_x86_64",
        "//platforms:linux_x86_64": "manylinux2014_x86_64",
        "//platforms:linux_arm64": "manylinux2014_aarch64",
    }),
    python_tag = "cp38",
    python_version = ">=3.8",
    requires = [
        "beautifulsoup4",
        "requests[socks]",
        "decorator",
        "protobuf",
        'orjson; platform_machine == "x86_64"',
        'psutil; sys_platform == "win32"',
        'distro; sys_platform != "darwin" and sys_platform != "win32"',
    ],
    strip_path_prefixes = [
        "pylib/",
    ],
    tags = ["manual"],
    version = anki_version,
    visibility = ["//visibility:public"],
    deps = [":anki_pkg"],
)

filegroup(
    name = "py_source_files",
    srcs = _py_srcs,
    visibility = [
        "//pylib:__subpackages__",
        "//qt/po:__pkg__",
    ],
)
